<template>
  <Spin :spinning="loading" tip="Loading...">
    <div>
      <!-- 索赔扣款信息查询-详情页面 -->
      <BreakSpace label="索赔扣款基本信息" :containerStyle="{ marginBottom: '10px' }" />
      <FormPanel ref="formPanel1" :list="formList" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <BaseDialog :visible.sync="suopeidan" :title="'索赔单号选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <SelectSuopeiDan @close="suopeidanClose" @save="suopeidanSave"></SelectSuopeiDan>
      </BaseDialog>
      <BreakSpace label="维修项目列表" :containerStyle="{ marginBottom: '10px' }" />
      <FilterTable
        ref="weixiuxiangmutable"
        :columns="columns"
        :params="fetchParams"
        :dataSource="dataList"
        :datakey="'items'"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="selectChange"
        :isSelectColumn="false"
        :isPagination = false
        height="auto"
      >
      </FilterTable>
      <div style="margin-bottom: 1rem;"></div>
      <!-- 备件换上件列表 -->
      <BreakSpace label="备件换上件列表" :containerStyle="{ marginBottom: '10px' }" />
      <FilterTable
        ref="peijianhuanshangjiantable"
        :columns="columns1"
        :params="fetchParams"
        :dataSource="dataList1"
        :datakey="'items'"
        :onColumnsChange="columns1 => (this.columns1 = columns1)"
        :onRowSelectChange="selectChange"
        :isSelectColumn="false"
        :isPagination = false
        height="auto"
      >

      </FilterTable>
      <!-- 旧件列表 -->
      <BreakSpace label="旧件列表" :containerStyle="{ marginBottom: '10px' }" />
      <FilterTable
        ref="peijianhuanxiajiantable"
        :columns="columnshuanxiajian"
        :params="fetchParams"
        :dataSource="huanxiajiandataList"
        :datakey="'items'"
        :onColumnsChange="columnshuanxiajian => (this.columnshuanxiajian = columnshuanxiajian)"
        :onRowSelectChange="selectChange"
        :isSelectColumn="false"
        :isPagination = false
        height="auto"
      >
      </FilterTable>
      <!-- 附加项目列表 -->
      <BreakSpace label="附加项目列表" :containerStyle="{ marginBottom: '10px' }" />
      <FilterTable
        ref="fujiaxiangmutable"
        :columns="columnsfujiaxiangmu"
        :params="fetchParams"
        :dataSource="fujiaxiangmudataList"
        :datakey="'items'"
        :onColumnsChange="columnsfujiaxiangmu => (this.columnsfujiaxiangmu = columnsfujiaxiangmu)"
        :onRowSelectChange="selectChange"
        :isSelectColumn="false"
        :isPagination = false
        height="auto"
      >
      </FilterTable>
      <!--申请费用信息  -->
      <!--    <BreakSpace label="申请费用信息" :containerStyle="{ marginBottom: '10px' }" />-->
      <!--    <FormPanel ref="formPanel2" :list="shenqingfeiyongformList" :cols="4" :formType="'add'" labelWidth="110" @formChange="formChangeHandle1" :isSubmitBtn="false" />-->
      <BreakSpace label="外出救援费用" :containerStyle="{ marginBottom: '10px' }" />
      <FormPanel ref="formPanelOutAmount" :list="outAmountformList" :cols="5" :formType="'add'" labelWidth="110" @formChange="formChangeHandleOutAmount" :isSubmitBtn="false" />

      <!--审核费用信息  -->
      <BreakSpace label="审核费用信息" :containerStyle="{ marginBottom: '10px' }" />
      <FormPanel ref="formPanel3" :list="shenhefeiyongformList" :cols="4" :formType="'add'" labelWidth="110" @formChange="formChangeHandle2" :isSubmitBtn="false" />
      <!--扣款信息  -->
      <BreakSpace label="扣款信息" :containerStyle="{ marginBottom: '10px' }" />
      <FormPanel ref="formPanel4" :list="koukuanfeiyongformList" :cols="4" :formType="'add'" labelWidth="110" @formChange="formChangeHandle3" :isSubmitBtn="false" />
      <!--    <LazyLoadTab v-model="activeName" :tabMenus="menus"></LazyLoadTab>-->
      <div style="padding-top: 3rem;"></div>
      <div
        :style="{
        position: 'fixed',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
      >
        <multiuse-button type="primary" @click="closeDialog">关 闭</multiuse-button>
      </div>
    </div>
  </Spin>
</template>
<script>
import { queryRpCmsClaimCb } from '@/api/claim/factory/ClaimDebitsInfoMaintain';
import { isFormEmpty } from '@/utils/index';
import SelectSuopeiDan from './SelectSuopeiDan.vue';
import { dictionary } from '@/utils/dictMixin';
import { getRescueList } from '@/api/customerRepair/rescueManagement/rescueOrder';

export default {
  props: ['rowData'],
  mixins: [dictionary],
  components: {
    SelectSuopeiDan
  },
  data() {
    return {
      index: 0,
      loading: false,
      dataList: [],
      dataList1: [],
      huanxiajiandataList: [],
      fujiaxiangmudataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      columns: this.creatHeader(),
      columns1: this.creatHeader1(),
      columnshuanxiajian: this.columnshuanxiajian1(),
      columnsfujiaxiangmu: this.columnsfujiaxiangmu1(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中服务中心
      formList: this.createFormList(),
      suopeidan: false,
      // shenqingfeiyongformList: this.shenqingfeiyong(),
      outAmountformList: this.outAmountformListFn(),
      shenhefeiyongformList: this.shenhefeiyong(),
      koukuanfeiyongformList: this.koukuanxinxi(),
      saveData: {},
      saveData1:null,
      saveData2:null,
      saveData3:null,
      saveData4:null,
      saveData5:null,
      saveDataOut:null,
      flag:false,
    };
  },
  async created() {
    this.loading = true;
    await this.chuqi();

    await this.getRpCmsClaimCbs(this.rowData.cbId);

    this.loading = false;
  },
  mounted() {
    // this.getRpCmsClaimCb(this.rowData.cbId);

  },
  methods: {

    async chuqi() {
      // //扣款单状态
      // const res = await getPartType({ type: 5591 });
      // if (res.resultCode == 200) {
      //   this.formList.find(x => x.fieldName == 'cbStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }

    },
    async getRpCmsClaimCbs(cbId) {
      let res = await queryRpCmsClaimCb(cbId);
      let data = res.data;
      this.claimId = data.claimId;
      this.ownerCode = data.ownerCode;
      this.ownerParCode = data.ownerParCode;
      this.cbId = data.cbId;
      if(data.cbStatus==55911001 ){
        this.flag=true;
      }else {
        this.flag=false;
      }
      //为4个form赋值
      for (let i in data) {
        this.formList.map(y => {
          if (i == y.fieldName) {
            y.initialValue = data[i];
          }
        });
      }
      for (let i in data.claimDTO) {
        // this.shenqingfeiyongformList.map(y => {
        //   if (i == y.fieldName) {
        //     y.initialValue = data.claimDTO[i];
        //   }
        // });
        this.shenhefeiyongformList.map(y => {
          if (i == y.fieldName) {
            y.initialValue = parseFloat(data.claimDTO[i]).toFixed(2);
          }
        });
        this.koukuanfeiyongformList.map(y => {
          if (i == y.fieldName) {
            y.initialValue = parseFloat(data.claimDTO[i]).toFixed(2);
          }
        });
        //剩余费用赋值
        this.koukuanfeiyongformList.map(x => {
          if('partResidue' == x.fieldName) {
            x.initialValue = parseFloat(data.partResidue).toFixed(2);
          }
          if('serviceResidue' == x.fieldName) {
            x.initialValue = parseFloat(data.serviceResidue).toFixed(2);
          }
        });
      }
      //为table赋值
      if (200 == res.resultCode) {
        let data = res.data;
        if (isFormEmpty(data)) {
          data = {};
        }
        //维修项目
        let records = data.labourList;
        if (isFormEmpty(records)) {
          records = [];
        }else{
          records.forEach(x => {
            x.cmsClaimPrice= parseFloat(x.cmsClaimPrice).toFixed(2);
            x.cmsClaimAmount= parseFloat(x.cmsClaimAmount).toFixed(2);
            x.deductionAmount= parseFloat(x.deductionAmount).toFixed(2);
          });
        }
        this.$refs.weixiuxiangmutable.$refs.pageTable.EXECUTE_DELETE(this.$refs.weixiuxiangmutable.$refs.pageTable.list);
        this.$refs.weixiuxiangmutable.$refs.pageTable.EXECUTE_INSERT(records);
        //附加项目
        let records1 = data.addList;
        if (isFormEmpty(records1)) {
          records1 = [];
        }else{
          records1.forEach(x => {
            x.addAmount= parseFloat(x.addAmount).toFixed(2);
            x.deductionAmount= parseFloat(x.deductionAmount).toFixed(2);
          });
        }
        this.$refs.fujiaxiangmutable.$refs.pageTable.EXECUTE_DELETE(this.$refs.fujiaxiangmutable.$refs.pageTable.list);
        this.$refs.fujiaxiangmutable.$refs.pageTable.EXECUTE_INSERT(records1);
      }
      //故障件
      let records2 = data.partList;
      if (isFormEmpty(records2)) {
        records2 = [];
      }else{
        records2.forEach(x => {
          x.cmsClaimPrice= parseFloat(x.cmsClaimPrice).toFixed(2);
          x.cmsClaimAmount= parseFloat(x.cmsClaimAmount).toFixed(2);
        });
      }
      this.$refs.peijianhuanxiajiantable.$refs.pageTable.EXECUTE_DELETE(this.$refs.peijianhuanxiajiantable.$refs.pageTable.list);
      this.$refs.peijianhuanxiajiantable.$refs.pageTable.EXECUTE_INSERT(records2);
      //备件
      let records3 = data.partRpList;
      if (isFormEmpty(records3)) {
        records3 = [];
      }else{
        records3.forEach(x => {
          x.cmsClaimPrice= parseFloat(x.cmsClaimPrice).toFixed(2);
          x.cmsClaimAmount= parseFloat(x.cmsClaimAmount).toFixed(2);
          x.deductionAmount= parseFloat(x.deductionAmount).toFixed(2);
        });
      }
      this.$refs.peijianhuanshangjiantable.$refs.pageTable.EXECUTE_DELETE(this.$refs.peijianhuanshangjiantable.$refs.pageTable.list);
      this.$refs.peijianhuanshangjiantable.$refs.pageTable.EXECUTE_INSERT(records3);
      if(null!=data.outId){
        let res = await getRescueList({outId: data.outId,current: 1,currentPage: 1,page: 1,pageNum: 1,pageSize: 10,size: 10}); // 救援单

        let records = res.data.records[0];
        for (let i in records) {
          this.outAmountformList.map(y => {
            if (i == y.fieldName) {
              if(y.fieldName!='rewardType'){
                y.initialValue = parseFloat(records[i]).toFixed(2);
              }else{
                y.initialValue = records[i];
              }
            }
          });
        }
        if(records!=null){
          this.shenhefeiyongformList.find(x => x.fieldName == 'helpAmount').initialValue =parseFloat(records.allAmount).toFixed(2);
        }
      }
      this.newVar = data;
    },

    formChangeHandle(val) {
      console.log(val);
      this.saveData1 = val;
    },
    formChangeHandle1(val) {
      console.log(val);
      this.saveData2 = val;
    },
    formChangeHandleOutAmount(val) {
      console.log(val);
      this.saveDataOut = val;
    },
    formChangeHandle2(val) {
      console.log(val);
      this.saveData3 = val;
    },
    formChangeHandle3(val) {
      console.log(val);
      this.saveData4 = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    // //保存
    // saveDialog() {
    //   this.$refs.formPanel1.SUBMIT_FORM();
    //   if (this.saveData) {
    //     this.$emit('save', this.selectedRows);
    //   }
    // },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    },
    selectRono() {
      this.suopeidan = true;
    },
    suopeidanClose() {
      this.suopeidan = false;
    },
    suopeidanSave(obj) {
      this.formList.find(x => x.fieldName == 'ownerCode').initialValue = obj[0].ownerCode;
      this.formList.find(x => x.fieldName == 'dealerName').initialValue = obj[0].dealerName;
      this.formList.find(x => x.fieldName == 'claimType').initialValue = obj[0].cmsClaimType;
      this.formList.find(x => x.fieldName == 'claimNo').initialValue = obj[0].claimNo;
      this.formList.find(x => x.fieldName == 'finalSumAmount').initialValue = obj[0].finalSumAmount;
      this.claimId = obj[0].claimId;
      this.getRpCmsClaimCb(this.claimId);
      this.suopeidan = false;
    },
    add() {},
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '索赔扣款单号',
          fieldName: 'cbNo',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '服务中心代码',
          fieldName: 'dealerCode',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '服务中心名称',
          fieldName: 'dealerName',
          initialValue: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '扣款单状态',
          fieldName: 'cbStatus',
          initialValue: '',
          itemList: this.createDictList('5502'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '索赔单号',
          fieldName: 'claimNo',
          initialValue: '',
          placeholder: '请输入标题名称...',
          readonly: true,
          disabled: true,
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.selectRono()} />
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '扣款原因',
          fieldName: 'cbReason',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '索赔类型',
          fieldName: 'claimType',
          itemList:this.createDictList('5510',[]),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '现结算金额',
          fieldName: 'finalSumAmount',
          initialValue: '',
          hidden:true,
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remarks',
          selfCols:3,
          placeholder: '请输入备注...',
          disabled: true
        }
      ];
    },

    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '维修项目代码', dataIndex: 'labourCode', sorter: true },
        { title: '维修项目名称', dataIndex: 'labourName', sorter: true },
        { title: '维修类型', dataIndex: 'repairTypeName', sorter: true },
        { title: '标准工时', dataIndex: 'labourHours', sorter: true },
        { title: '索赔工时单价', dataIndex: 'cmsClaimPrice', sorter: true },
        { title: '索赔金额', dataIndex: 'cmsClaimAmount', sorter: true },
        // { title: '审核金额', dataIndex: 'checkAmount', sorter: true},
        {title: '扣款金额', dataIndex: 'deductionAmount',sorter: true }
      ];
    },
    //table表头和数据源
    creatHeader1() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '备件代码', dataIndex: 'partCode', sorter: true },
        { title: '备件名称', dataIndex: 'partName', sorter: true },
        { title: '单位', dataIndex: 'partUnit', sorter: true },
        { title: '数量', dataIndex: 'partCount', sorter: true },
        { title: '索赔单价', dataIndex: 'cmsClaimPrice', sorter: true },
        { title: '索赔总价', dataIndex: 'cmsClaimAmount', sorter: true },
        // { title: '调整后金额', dataIndex: 'checkAmount', sorter: true},
        {title: '扣款金额', dataIndex: 'deductionAmount',sorter: true }


      ];
    },
    //换下件
    columnshuanxiajian1() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '备件代码', dataIndex: 'partCode', sorter: true },
        { title: '备件名称', dataIndex: 'partName', sorter: true },
        { title: '变更前备件代码', dataIndex: 'partCodeBefore', sorter: true },
        { title: '变更前备件名称', dataIndex: 'partNameBefore', sorter: true },
        { title: '单位', dataIndex: 'partUnit', sorter: true },
        { title: '数量', dataIndex: 'partCount', sorter: true },
        { title: '单价', dataIndex: 'cmsClaimPrice', sorter: true },
        { title: '金额', dataIndex: 'cmsClaimAmount', sorter: true },
        { title: '备件回运类型', dataIndex: 'returnType', sorter: true,dictItems:this.createDictList('5585'), },
        { title: '采购渠道代码', dataIndex: 'channelCode', sorter: true  },
      ];
    },
    //附加项目
    columnsfujiaxiangmu1() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '附加项目代码', dataIndex: 'addCode', sorter: true },
        { title: '附加项目名称', dataIndex: 'addName', sorter: true },
        { title: '索赔金额', dataIndex: 'addAmount', sorter: true },
        // { title: '审核金额', dataIndex: 'checkAmount', sorter: true },
        {title: '扣款金额', dataIndex: 'deductionAmount',sorter: true },
        { title: '备注', dataIndex: 'addRemark', sorter: true }
      ];
    },
    //外出救援费用
    outAmountformListFn() {
      return [
        {
          type: 'INPUT',
          label: '燃油补贴',
          fieldName: 'fuelSubsidy',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '人员补贴',
          fieldName: 'prSubsidy',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '餐补',
          fieldName: 'foodSubsidy',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '住宿补贴',
          fieldName: 'staySubsidy',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '车船费',
          fieldName: 'crossAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '拖车费',
          fieldName: 'trailerAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '其他费用',
          fieldName: 'otherAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'SELECT',
          label: '激励类型',
          fieldName: 'rewardType',
          initialValue: '',
          itemList: this.createDictList('5537',[]),
          disabled:true,
          hidden:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '奖励金额',
          fieldName: 'reward',
          initialValue: '',
          disabled:true,
          hidden:true,
          readonly: true // 只读
        }
      ];
    },
    //审核费用信息
    shenhefeiyong() {
      return [
        {
          type: 'INPUT',
          label: '合计费用',
          fieldName: 'claimSumAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '工时费用',
          fieldName: 'claimLabourAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '备件费用',
          fieldName: 'claimPartAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '备件管理费用',
          fieldName: 'partManageAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '附加项目费用',
          fieldName: 'claimAddAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '外出费用',
          fieldName: 'helpAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: 'PDI费用',
          fieldName: 'priceAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '扣款金额',
          fieldName: 'outCbAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        }
      ];
    },//扣款信息
    koukuanxinxi() {
      return [
        {
          type: 'INPUT',
          label: '合计扣款',
          fieldName: 'cbSumAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '工时扣款',
          fieldName: 'cbLabourAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '备件扣款',
          fieldName: 'cbPartAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '附加项目扣款',
          fieldName: 'cbAddAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '外出费用扣款',
          fieldName: 'cbOutAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '物流费用扣款',
          fieldName: 'cbTravelAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '备件管理费用扣款',
          fieldName: 'cbPtManageAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '其他费用扣款',
          fieldName: 'cbOtherAmount',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '材料剩余金额',
          fieldName: 'partResidue',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '服务剩余金额',
          fieldName: 'serviceResidue',
          initialValue: '0.00',
          disabled:true,
          readonly: true // 只读
        },
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
